<template>
  <div class="student-home-container">
    <!-- 学习统计 -->
    <el-row :gutter="20" class="stats-row">
      <el-col :span="8">
        <el-card class="stat-card">
          <div class="stat-icon">
            <el-icon><Clock /></el-icon>
          </div>
          <div class="stat-data">
            <p class="stat-title">本周学习时长</p>
            <p class="stat-value">{{ studyStats.weeklyHours }}小时</p>
            <p class="stat-desc">较上周 <span class="increase">+{{ studyStats.weeklyIncrease }}小时</span></p>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="stat-card">
          <div class="stat-icon">
            <el-icon><Notebook /></el-icon>
          </div>
          <div class="stat-data">
            <p class="stat-title">已完成课程</p>
            <p class="stat-value">{{ studyStats.finishedCourses }}门</p>
            <p class="stat-desc">本月目标 <span class="target">{{ studyStats.monthlyTarget }}门</span></p>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="stat-card">
          <div class="stat-icon">
            <el-icon><StarFilled /></el-icon>
          </div>
          <div class="stat-data">
            <p class="stat-title">测验平均分</p>
            <p class="stat-value">{{ studyStats.averageScore }}分</p>
            <p class="stat-desc">较上月 <span class="increase">+{{ studyStats.scoreIncrease }}分</span></p>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 最近课程和待完成作业 -->
    <el-row :gutter="20" class="content-row">
      <!-- 最近课程 -->
      <el-col :span="12">
        <el-card class="content-card">
          <template #header>
            <span>最近学习课程</span>
            <el-button size="small" link @click="gotoAllCourses">查看全部</el-button>
          </template>
          <el-table :data="recentCourses" border>
            <el-table-column prop="title" label="课程名称" width="200"></el-table-column>
            <el-table-column prop="subject" label="科目"></el-table-column>
            <el-table-column prop="progress" label="进度">
              <template #default="scope">
                <el-progress :percentage="scope.row.progress" :stroke-width="6" style="width: 120px;"></el-progress>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template #default="scope">
                <el-button 
                  size="small" 
                  type="primary" 
                  @click="continueStudy(scope.row.id)"
                >
                  继续学习
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>

      <!-- 待完成作业 -->
      <el-col :span="12">
        <el-card class="content-card">
          <template #header>
            <span>待完成作业</span>
            <span class="homework-count">{{ pendingHomework.length }} 项未完成</span>
          </template>
          <el-table :data="pendingHomework" border>
            <el-table-column prop="title" label="作业名称" width="200"></el-table-column>
            <el-table-column prop="subject" label="科目"></el-table-column>
            <el-table-column prop="deadline" label="截止时间"></el-table-column>
            <el-table-column label="操作">
              <template #default="scope">
                <el-button 
                  size="small" 
                  type="primary" 
                  @click="gotoHomework(scope.row.id)"
                >
                  去完成
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <!-- 教师通知 -->
    <el-card class="notices-card">
      <template #header>
        <span>教师通知</span>
        <el-button size="small" link>查看全部</el-button>
      </template>
      <el-timeline>
        <el-timeline-item 
          v-for="(notice, index) in recentNotices" 
          :key="index"
          :timestamp="notice.time"
        >
          <el-card>
            <h4>{{ notice.title }}</h4>
            <p>{{ notice.content }}</p>
            <p class="notice-sender">—— {{ notice.teacherName }}</p>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useLoginStore } from '../../../stores/index.js';
import { useRouter } from 'vue-router';
import { Notebook, ReadingLamp, Document } from '@element-plus/icons-vue'

// 路由实例
const router = useRouter();

// 获取登录用户信息
const loginStore = useLoginStore();
const user = loginStore.user;

// 今日日期
const today = ref('');

// 学习统计数据
const studyStats = ref({
  weeklyHours: 5.2,
  weeklyIncrease: 1.8,
  finishedCourses: 4,
  monthlyTarget: 8,
  averageScore: 89,
  scoreIncrease: 4
});

// 最近课程
const recentCourses = ref([
  {
    id: 1,
    title: '小学语文阅读理解技巧',
    subject: '语文',
    progress: 60
  },
  {
    id: 2,
    title: '三年级数学思维训练',
    subject: '数学',
    progress: 100
  },
  {
    id: 3,
    title: '小学英语入门',
    subject: '英语',
    progress: 30
  }
]);

// 待完成作业
const pendingHomework = ref([
  {
    id: 1,
    title: '阅读理解练习（一）',
    subject: '语文',
    deadline: '2023-10-20'
  },
  {
    id: 2,
    title: '混合运算练习题',
    subject: '数学',
    deadline: '2023-10-18'
  }
]);

// 最近通知
const recentNotices = ref([
  {
    id: 1,
    title: '关于下周数学单元测验的通知',
    content: '下周三将进行数学第三单元测验，请认真复习',
    time: '2023-10-15 09:30',
    teacherName: '李老师（数学）'
  },
  {
    id: 2,
    title: '班级读书分享会',
    content: '10月20日下午将举行班级读书分享会，请准备好分享内容',
    time: '2023-10-14 14:20',
    teacherName: '张老师（班主任）'
  }
]);

// 初始化日期
onMounted(() => {
  const date = new Date();
  today.value = date.toLocaleDateString('zh-CN', { 
    year: 'numeric', 
    month: 'long', 
    day: 'numeric',
    weekday: 'long'
  });
});

// 前往全部课程
const gotoAllCourses = () => {
  router.push('/student/course');
};

// 继续学习课程
const continueStudy = (courseId) => {
  router.push(`/student/course/detail/${courseId}`);
};

// 前往作业页面
const gotoHomework = (homeworkId) => {
  console.log('前往作业:', homeworkId);
  // 实际项目中跳转到作业详情页
};
</script>

<style scoped>
.student-home-container {
  padding: 20px;
}

.welcome-card {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff4e5;
}

.welcome-content h2 {
  color: #e67e22;
  margin-bottom: 10px;
}

.stats-row {
  margin-bottom: 20px;
}

.stat-card {
  display: flex;
  align-items: center;
  
  height: 100%;
}

.stat-icon {
  font-size: 24px;
  color: #FF9F1C;
  margin-right: 15px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(255, 159, 28, 0.1);
  border-radius: 50%;
}

.stat-data {
  flex-grow: 1;
}

.stat-title {
  color: #666;
  font-size: 14px;
  margin-bottom: 5px;
}

.stat-value {
  font-size: 22px;
  font-weight: bold;
  color: #333;
}

.stat-desc {
  font-size: 12px;
  color: #666;
}

.increase {
  color: #00B4A0;
}

.target {
  color: #1890ff;
}

.content-row {
  margin-bottom: 20px;
}

.content-card :deep(.el-card__header) {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.homework-count {
  color: #f56c6c;
  font-size: 14px;
}

.notices-card {
  margin-top: 20px;
}

.notice-sender {
  color: #666;
  font-size: 12px;
  margin-top: 5px;
  text-align: right;
}
</style>